package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Api(tags = "物资类别")
@RequestMapping("/business/productCategory")

public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;


    /**
     * 分页显示所有物资分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/categoryTree")
    @ApiOperation("获取物资分类列表")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree(Integer pageNum,
                                     Integer pageSize
                                     ){
        List<ProductCategoryTreeNodeVO> allCategory = productCategoryService.findAllCategory();
        if (pageNum!=null&&pageSize!=null) {
            allCategory= ListPageUtils.page(allCategory, pageSize, pageNum);
        }
        if (!CollectionUtils.isEmpty(allCategory)) {
            PageVO<ProductCategoryTreeNodeVO> pageVO = new PageVO<>(allCategory.size(), allCategory);
            return ResponseBean.success(pageVO);
        }
        return null;

    }

    @GetMapping("/getParentCategoryTree")
    @ApiOperation("获取父分类列表")
    public ResponseBean getParentCategoryTree(){
        //调用service方法
        List<ProductCategoryTreeNodeVO> parentList =  productCategoryService.findParentCategory();
        return  ResponseBean.success(parentList);
    }

    /**
     * 添加分类
     * @param productCategory
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加分类")
    public ResponseBean add (@RequestBody ProductCategory productCategory){
        //调用service方法
        productCategoryService.addProductCategory(productCategory);
        return ResponseBean.success();
    }

    /**
     * 根据id查询物资分类（回显）
     * @param id
     * @return
     */
    @ApiOperation("根据id查询物资分类（回显）")
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        //
        ProductCategory productCategory = productCategoryService.edit(id);
        return ResponseBean.success(productCategory);
    }

    /**
     * 修改分类信息
     * @param productCategory
     * @return
     */
    @ApiOperation("修改分类信息")
    @PutMapping("/update/{id}")
    public ResponseBean updateById(@RequestBody ProductCategory productCategory){
        productCategoryService.update(productCategory);
        return ResponseBean.success();
    }

    /**
     * 根据id删除分类信息
     * @return
     */
    @ApiOperation("删除分类信息")
    @DeleteMapping("/delete/{id}")
    public ResponseBean deleteById(@PathVariable Long id){
      String message =  productCategoryService.delete(id);
      if (message.equalsIgnoreCase("1")){
          return ResponseBean.success();
      }
       return ResponseBean.error("该节点有子节点无法删除");
    }

}
